package cn.com.dycc.party_dues.info.service;




import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.com.dycc.common.entity.DeleteEntity;
import cn.com.dycc.common.entity.Party_Org_MemberPart;
import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.common.page.BaseParam;
import cn.com.dycc.common.wxpay.WXPay;
import cn.com.dycc.common.wxpay.WXPayConfigImpl;
import cn.com.dycc.common.wxpay.WXPayUtil;
import cn.com.dycc.party_dues.info.dao.Pb_Party_DuesDao;
import cn.com.dycc.party_dues.info.entity.App_MyParty_Dues;
import cn.com.dycc.party_dues.info.entity.App_MyParty_DuesNew;
import cn.com.dycc.party_dues.info.entity.App_Party_Dues;
import cn.com.dycc.party_dues.info.entity.App_Party_DuesDetail;
import cn.com.dycc.party_dues.info.entity.Dues_Month;
import cn.com.dycc.party_dues.info.entity.Import_Party_Dues;
import cn.com.dycc.party_dues.info.entity.Pb_Party_Dues;
import cn.com.dycc.party_dues.info.param.Pb_Party_DuesParam;
import cn.com.dycc.party_dues.pay_log.dao.Pb_Party_Dues_Pay_LogDao;
import cn.com.dycc.party_dues.pay_log.entity.Pb_Party_Dues_Pay_Log;
import cn.com.dycc.party_dues.pay_online.dao.Pb_Party_Dues_Pay_OnlineDao;
import cn.com.dycc.party_dues.remind.dao.RemindDao;
import cn.com.dycc.party_dues.remind.entity.Remind;
import cn.com.dycc.party_dues.remind.param.RemindParam;
import cn.com.dycc.party_org.member.dao.MemberDao;
import cn.com.dycc.util.ConfigRegex;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.ExcelUtil;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.PropertiesUtil;
import cn.com.dycc.util.StringUtil;

@Service("pb_Party_DuesService")
@Transactional
public class Pb_Party_DuesServiceImpl implements Pb_Party_DuesService{
	
	@Resource
	private Pb_Party_DuesDao pb_Party_DuesDao;
	
	
	@Resource
	private MemberDao memberDao;
	
	@Resource
	private Pb_Party_Dues_Pay_LogDao pb_Party_Dues_Pay_LogDao;
	
	@Resource
	private Pb_Party_Dues_Pay_OnlineDao pb_Party_Dues_Pay_OnlineDao;
	
	@Resource
	private RemindDao remindDao;
	
	
	@SystemServiceLog(description=Constants.PB_PARTY_DUES_ADD)
	public int add(BaseParam param, HttpServletRequest request) throws RuntimeException {
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		checkParam(param0);
		HttpSession session=request.getSession();
		String party_dept_id=(String)session.getAttribute("dept_id");
		if(party_dept_id==null||party_dept_id.trim().isEmpty()){
			throw new Pb_Party_DuesNotFoundException("操作员不是党员");
		}
		StringBuilder builder=new StringBuilder();
		if(param0.getDues_month()==null||param0.getDues_month().trim().isEmpty()){
			builder.append("党费（年）月份不能为空;");
		}
		if((param0.getUser_id()==null||param0.getUser_id().trim().isEmpty())&&(param0.getUser_name()==null||param0.getUser_name().trim().isEmpty())){
			builder.append("党员员工不能为空;");
		}
		/*if((param0.getParty_dept_id()==null||param0.getParty_dept_id().trim().isEmpty())&&(param0.getParty_dept_name()==null||param0.getParty_dept_name().trim().isEmpty())){
			builder.append("党组不能为空;");
		}
		if((param0.getDept_id()==null||param0.getDept_id().trim().isEmpty())&&(param0.getDept_name()==null||param0.getDept_name().trim().isEmpty())){
			builder.append("归属单位不能为空;");
		}*/
		if(param0.getDues_val()==null||param0.getDues_val().trim().isEmpty()){
			builder.append("党费金额不能为空;");
		}
		if(param0.getDues_type()==null||param0.getDues_type().trim().isEmpty()){
			builder.append("党费类别不能为空;");
		}
		if(param0.getPay_flag()==null||param0.getPay_flag().trim().isEmpty()){
			builder.append("缴纳标识不能为空;");
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Party_DuesNotFoundException(builder.toString());
		}
		List<Party_Org_MemberPart> list=new ArrayList<Party_Org_MemberPart>();
		if(param0.getUser_id()!=null&&!param0.getUser_id().trim().isEmpty()){
			list=memberDao.findMemBerByIdOrName(param0.getUser_id(), null);
		}else {
			list=memberDao.findMemBerByIdOrName(null, param0.getUser_name());
		}
		if(list.isEmpty()){
			String error="新增党费对应的党员在系统中不存在;";
			StringBuilder builder2 =new StringBuilder(error);
			if(param0.getUser_name()!=null&&!param0.getUser_name().trim().isEmpty()){
				builder2.insert(9, param0.getUser_name());
			}
			throw new Pb_Party_DuesNotFoundException(builder2.toString());
		}else if (list.size()!=1) {
			String error="新增党费对应的党员在系统中存在同名的党员;";
			StringBuilder builder2 =new StringBuilder(error);
			if(param0.getUser_name()!=null&&!param0.getUser_name().trim().isEmpty()){
				builder2.insert(9, param0.getUser_name());
			}
			throw new Pb_Party_DuesNotFoundException(builder2.toString());
		}
		String opt_user_id=(String)session.getAttribute("id");
		String org_id=(String)session.getAttribute("org_id");
		//校验页面传的值与系统中对应值是否相同
		param0=contrastParam(param0, list.get(0));
		//检验新增党员所对应的月份的党费信息是否存在
		Pb_Party_DuesParam param1=new Pb_Party_DuesParam();
		if("1".equals(param0.getDues_type())){
			param1.setDues_type(param0.getDues_type());;
		}else{
			param1.setExcept_dues_type("1");
		}		
		param1.setUser_id(param0.getUser_id());
		param1.setDues_month(param0.getDues_month());
		param1.setOrg_id(org_id);
		String count=pb_Party_DuesDao.getCountByParam(param1);
		if(!"0".equals(count)){
			String error="新增党员,党费信息已存在;";
			StringBuilder builder2 =new StringBuilder(error);
			if(param0.getUser_name()!=null&&!param0.getUser_name().trim().isEmpty()){
				builder2.insert(4, param0.getUser_name());
			}
			builder2.insert(builder2.indexOf("党费"), param0.getDues_month());
			throw new Pb_Party_DuesNotFoundException(builder2.toString());
		}
		
		
		String create_date=DateUtil.getNowTimestamp().toString();
		
		param0.setOpt_user_id(opt_user_id);
		param0.setCreate_date(create_date);
		param0.setState("Y");
		param0.setOrg_id(org_id);
		
		List<Object> lists = getServiceList(param0);
		Pb_Party_Dues entity=(Pb_Party_Dues)lists.get(0);
		int d = pb_Party_DuesDao.add(entity);
		if(d>0){
			if("2".equals(param0.getPay_flag())){
				addPartyDuesLog(pb_Party_Dues_Pay_OnlineDao, memberDao, pb_Party_Dues_Pay_LogDao, entity.getCreate_date(), entity);
			}
		}
		
		
		return d;
	
	}
	
	/**
	 * 增加成功缴费的党费日志
	 * @param pb_Party_Dues_Pay_OnlineDao
	 * @param memberDao
	 * @param pb_Party_Dues_Pay_LogDao
	 * @param current_date
	 * @param entity
	 */
	private void addPartyDuesLog(Pb_Party_Dues_Pay_OnlineDao pb_Party_Dues_Pay_OnlineDao,MemberDao memberDao,Pb_Party_Dues_Pay_LogDao pb_Party_Dues_Pay_LogDao,Timestamp current_date,Pb_Party_Dues entity){
		Pb_Party_Dues_Pay_Log log=new Pb_Party_Dues_Pay_Log();
		String pay_id=pb_Party_Dues_Pay_OnlineDao.getPayId();
		List<Party_Org_MemberPart> list2=memberDao.findMemBerByIdOrName(entity.getOpt_user_id(), null);
		Party_Org_MemberPart part=list2.get(0);
		log.setPay_id(pay_id);
		log.setDues_id(entity.getDues_id());
		log.setDues_month(entity.getDues_month());
		log.setUser_id(entity.getUser_id());
		log.setUser_name(entity.getUser_name());
		log.setParty_dept_id(entity.getParty_dept_id());
		log.setParty_dept_name(entity.getParty_dept_name());
		log.setDept_id(entity.getDept_id());
		log.setDept_name(entity.getDept_name());
		log.setDues_type(entity.getDues_type());
		log.setPay_type(1);
		log.setPay_val(entity.getDues_val());
		log.setPay_date(current_date);
		log.setPay_ok_date(current_date);
		log.setPay_des("");
		log.setCollector_id(part.getUser_id());
		log.setCollector_name(part.getUser_name());
		log.setCollector_party_id(part.getParty_dept_id());
		log.setCollector_party_name(part.getParty_dept_name());
		log.setCollector_dept_id(part.getDept_id());
		log.setCollector_dept_name(part.getDept_name());
		log.setState(entity.getState());
		log.setOrg_id(entity.getOrg_id());
		log.setCreate_date(current_date);
		log.setOpt_user_id(entity.getOpt_user_id());							
		pb_Party_Dues_Pay_LogDao.add(log);
	}
	
	/**
	 * 对比系统党员的基本信息与界面传的对应值是否存在差异
	 * @param param
	 * @param part
	 * @return
	 */
	private Pb_Party_DuesParam contrastParam(Pb_Party_DuesParam param,Party_Org_MemberPart part){
		StringBuilder builder=new StringBuilder();
		if(param.getUser_id()!=null&&!param.getUser_id().trim().isEmpty()){
			if(!param.getUser_id().equals(part.getUser_id())){
				builder.append("user_id值与系统中的值不相同;");
			}		
		}else {
			param.setUser_id(part.getUser_id());
		}
		if(param.getUser_name()!=null&&!param.getUser_name().trim().isEmpty()){
			if(!param.getUser_name().equals(part.getUser_name())){
				builder.append("user_name值与系统中的值不相同;");
			}			
		}else {
			param.setUser_name(part.getUser_name());
		}
		if(param.getParty_dept_id()!=null&&!param.getParty_dept_id().trim().isEmpty()){
			if(!param.getParty_dept_id().equals(part.getParty_dept_id())){
				builder.append("party_dept_id值与系统中的值不相同;");
			}			
		}else {
			param.setParty_dept_id(part.getParty_dept_id());
		}
		if(param.getParty_dept_name()!=null&&!param.getParty_dept_name().trim().isEmpty()){
			if(!param.getParty_dept_name().equals(part.getParty_dept_name())){
				builder.append("party_dept_name值与系统中的值不相同;");
			}			
		}else {
			param.setParty_dept_name(part.getParty_dept_name());
		}
		if(param.getDept_id()!=null&&!param.getDept_id().trim().isEmpty()){
			if(!param.getDept_id().equals(part.getDept_id())){
				builder.append("dept_id值与系统中的值不相同;");
			}		
		}else {
			param.setDept_id(part.getDept_id());
		}	
		if(param.getDept_name()!=null&&!param.getDept_name().trim().isEmpty()){
			if(!param.getDept_name().equals(part.getDept_name())){
				builder.append("dept_name值与系统中的值不相同;");
			}		
		}else {
			param.setDept_name(part.getDept_name());
		}	
		if(!"".equals(builder.toString())){
			throw new Pb_Party_DuesNotFoundException(builder.toString());
		}
		return param;
	} 

	@SystemServiceLog(description=Constants.PB_PARTY_DUES_DELETE)
	public int delete(String[] attrStrs, HttpServletRequest request) throws RuntimeException {
		HttpSession session=request.getSession();
		String party_dept_id=(String)session.getAttribute("dept_id");
		String org_id=(String)session.getAttribute("org_id");
		if(party_dept_id==null||party_dept_id.trim().isEmpty()){
			throw new Pb_Party_DuesNotFoundException("操作员不是党员");
		}
		if (attrStrs == null) {
			throw new Pb_Party_DuesNotFoundException("请选择删除的党费信息");
		}
		StringBuilder builder=new StringBuilder();
		for(String dues_id:attrStrs){
			Pb_Party_DuesParam param=new Pb_Party_DuesParam();
			param.setDues_id(dues_id);
			param.setPay_flag("2");
			String count=pb_Party_DuesDao.getCountByParam(param);
			if(!"0".equals(count)){
				builder.append("党费记录"+dues_id+"已成功缴费,不能被删除;");
			}
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Party_DuesNotFoundException(builder.toString());
		}
		DeleteEntity deleteEntity=new DeleteEntity();
		deleteEntity.setInfo_ids(attrStrs);
		deleteEntity.setOrg_id(org_id);
		int d = pb_Party_DuesDao.delete(deleteEntity);
		
		return d;
		
	}

	@SystemServiceLog(description=Constants.PB_PARTY_DUES_UPDATE)
	public int update(BaseParam param, HttpServletRequest request) throws RuntimeException {
		HttpSession session=request.getSession();
		String party_dept_id=(String)session.getAttribute("dept_id");
		if(party_dept_id==null||party_dept_id.trim().isEmpty()){
			throw new Pb_Party_DuesNotFoundException("操作员不是党员");
		}
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		checkParam(param0);
		if (param0.getDues_id() == null || param0.getDues_id().trim().isEmpty()) {
			throw new Pb_Party_DuesNotFoundException("请选择要修改的党费信息");
		}
		Pb_Party_DuesParam param1=new Pb_Party_DuesParam();
		param1.setDues_id(param0.getDues_id());
		param1.setPay_flag("2");
		String count=pb_Party_DuesDao.getCountByParam(param1);
		if(!"0".equals(count)){
			throw new Pb_Party_DuesNotFoundException("将要修改的党费信息已缴费成功或不存在,修改失败");
		}
		
		String org_id=(String)session.getAttribute("org_id");
		param0.setOrg_id(org_id);
		List<Object> lists = getServiceList(param0);
		int d = pb_Party_DuesDao.update(lists);
		if(d>0){
			if("2".equals(param0.getPay_flag())){
				/*Pb_Party_Dues entity0=(Pb_Party_Dues)list2.get(0);
				Pb_Party_Dues entity1=(Pb_Party_Dues)lists.get(0);*/
				Pb_Party_DuesParam param2=new Pb_Party_DuesParam();
				param2.setDues_id(param0.getDues_id());
				param2.setStart(0);
				param2.setEnd(1);
				List<Object> list3=pb_Party_DuesDao.findByParam(param2);
				Pb_Party_Dues entity=(Pb_Party_Dues)list3.get(0);
				addPartyDuesLog(pb_Party_Dues_Pay_OnlineDao, memberDao, pb_Party_Dues_Pay_LogDao, DateUtil.getNowTimestamp(), entity);
			}
		}
		return d;
		
	}

	public JsonObjectResult findByParam(BaseParam param, HttpServletRequest request) throws RuntimeException {
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		List<Object> lists = new ArrayList<Object>();
		checkParam(param0);
		param0 = (Pb_Party_DuesParam)realParam(param0, request);
		String count = pb_Party_DuesDao.getCountByParam(param0);
		System.out.println("param:"+param0);
		System.out.println("count:"+count);

		if ("0".equals(count)) {
			return new JsonObjectResult(lists);
		}
		
		param0=(Pb_Party_DuesParam)PageObjectUtil.getMysqlPageObject(count, param0);
		lists = pb_Party_DuesDao.findByParam(param0);
		return new JsonObjectResult(count,lists);
	}
	
	

	private BaseParam realParam(BaseParam param, HttpServletRequest request) {
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;	
		if(param0.getUser_name()!=null&&!param0.getUser_name().trim().isEmpty()){
			param0.setUser_name(StringUtil.format_mh_str(param0.getUser_name()));
		}
		if(param0.getParty_dept_name()!=null&&!param0.getParty_dept_name().trim().isEmpty()){
			param0.setParty_dept_name(StringUtil.format_mh_str(param0.getParty_dept_name()));
		}
		if(param0.getDept_name()!=null&&!param0.getDept_name().trim().isEmpty()){
			param0.setDept_name(StringUtil.format_mh_str(param0.getDept_name()));
		}
		
		if(param0.getOrder()==null||param0.getOrder().isEmpty()||"1".equals(param0.getOrder())){
			param0.setOrder("p.create_date desc");
		}else if("2".equals(param0.getOrder())){
			param0.setOrder("p.create_date asc");
			
		}else if("3".equals(param0.getOrder())){
			param0.setOrder("p.dues_month desc");
			
		}else{
			param0.setOrder(null);//手动防止SQL注入
		}
		
		return param0;
	}

	private void checkParam(BaseParam param) {
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		if(param0==null){
			throw new Pb_Party_DuesNotFoundException("未登录或登录超时,请重新登录");
		}
		if(param0.getDues_month()!=null&&!param0.getDues_month().trim().isEmpty()){
			if(!param0.getDues_month().matches(ConfigRegex.DATEREGEX3)){
				throw new Pb_Party_DuesNotFoundException("党费（年）月份格式不正确,应为199001");
			}
		}
		
	}

	private List<Object> getServiceList(BaseParam param) {
		
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		Pb_Party_Dues entity=new Pb_Party_Dues();
		entity.setDues_id(param0.getDues_id());
		entity.setDues_month(param0.getDues_month());
		entity.setUser_id(param0.getUser_id());
		entity.setUser_name(param0.getUser_name());
		entity.setParty_dept_id(param0.getParty_dept_id());
		entity.setParty_dept_name(param0.getParty_dept_name());
		entity.setDept_id(param0.getDept_id());
		entity.setDept_name(param0.getDept_name());
		if(param0.getDues_val()!=null&&!param0.getDues_val().trim().isEmpty()){
			entity.setDues_val(BigDecimal.valueOf(Double.valueOf(param0.getDues_val())));
		}
		if(param0.getDues_type()!=null&&!param0.getDues_type().trim().isEmpty()){
			entity.setDues_type(Integer.valueOf(param0.getDues_type()));
		}
		if(param0.getPay_flag()!=null&&!param0.getPay_flag().trim().isEmpty()){
			entity.setPay_flag(Integer.valueOf(param0.getPay_flag()));
		}
				
		entity.setState(param0.getState());
		entity.setOrg_id(param0.getOrg_id());
		if(param0.getCreate_date()!=null&&!param0.getCreate_date().trim().isEmpty()){
			entity.setCreate_date(Timestamp.valueOf(param0.getCreate_date()));
		}		
		entity.setOpt_user_id(param0.getOpt_user_id());
		
		List<Object> list=new ArrayList<Object>();
		list.add(entity);
		return list;
	}

	public JsonObjectResult batchImport(String name, MultipartFile file) throws Pb_Party_DuesNotFoundException {
		ExcelUtil excelUtil=new ExcelUtil();
		
		Map<String, String> map=new HashMap<String, String>();
		map.put("user_name", "党员姓名");
		map.put("party_dept_name", "归属党组或支部名称");
		map.put("dues_month", "月份（如201810）");
		map.put("dues_val", "党费金额（元）");
		map.put("dues_type", "党费类别");
		
		List<Object> list=excelUtil.getExcelInfo(name, file,Import_Party_Dues.class, map);
		for(int i=0;i<list.size();i++){
			Import_Party_Dues entity=(Import_Party_Dues)list.get(i);
			if(entity.getUser_name()==null||entity.getUser_name().trim().isEmpty()||entity.getParty_dept_name()==null||entity.getParty_dept_name().trim().isEmpty()){
				entity.setFlag("0");
				continue;
			}
			if(entity.getDues_month()==null||entity.getDues_month().trim().isEmpty()||!entity.getDues_month().matches(ConfigRegex.DATEREGEX3)){
				entity.setFlag("0");
				continue;
			}
			String count=pb_Party_DuesDao.checkImportPartyDues(entity);
			if("0".equals(count)){
				entity.setFlag("0");
			}else{
				entity.setFlag("1");
			}
			list.set(i, entity);
		}
		JsonObjectResult result=new JsonObjectResult(list);
		return result;
	}

	@SystemServiceLog(description=Constants.PB_PARTY_DUES_ADD)
	public int saveExcel(String infos, HttpServletRequest request) throws Pb_Party_DuesNotFoundException {
		StringBuilder builder=new StringBuilder();
		String[] save_attributes={"user_name","party_dept_name","dues_month","dues_val","dues_type","flag"};
		List<Object> list0=StringUtil.getObjectByJson(infos, Import_Party_Dues.class, save_attributes);
		List<Pb_Party_DuesParam> param_lists=new ArrayList<Pb_Party_DuesParam>();
		for(Object object:list0){
			Import_Party_Dues entity=(Import_Party_Dues)object;
			if(entity.getFlag()==null||entity.getFlag().trim().isEmpty()||!"1".equals(entity.getFlag())){
				throw new Pb_Party_DuesNotFoundException("导入数据存在异常数据,导入失败");
			}
			Pb_Party_DuesParam param=new Pb_Party_DuesParam();
			param.setUser_name(entity.getUser_name());
			param.setParty_dept_name(entity.getParty_dept_name());
			param.setDues_month(entity.getDues_month());
			param.setDues_val(entity.getDues_val());
			param.setDues_type(entity.getDues_type());
			param.setPay_flag("0");
			param_lists.add(param);
		}
		for(Pb_Party_DuesParam param:param_lists){
			try {
				add(param, request);
			} catch (Exception e) {
				if(StringUtil.isContainChinese(e.getMessage())){
					builder.append(e.getMessage());
				}
			}
			
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Party_DuesNotFoundException(builder.toString());
		}
		return list0.size();
	}

	public JsonObjectResult findAppDuesByParam(BaseParam param, HttpServletRequest request)
			throws Pb_Party_DuesNotFoundException {
		JsonObjectResult result=findByParam(param, request);
		HttpSession session=request.getSession();
		String dues_month_min=(String)session.getAttribute("dues_month_min");
		String dues_month_max=(String)session.getAttribute("dues_month_max");
		List<Pb_Party_Dues> list=(List<Pb_Party_Dues>)result.getData();
		if(list.size()==0){
			return result;
		}
		
		Map<String, String> month_map=new HashMap<String, String>();
		
		List<Object> object_list=new ArrayList<Object>();
		//遍历list 获得缴纳党费信息的缴纳月份
		for(Pb_Party_Dues dues:list){
			if(!month_map.containsKey(dues.getDues_month())){
				month_map.put(dues.getDues_month(), dues.getDues_month());
			}
		}
		String minDate=null;
		String maxDate=null;
		Set<String> keys=month_map.keySet();
		//获得缴纳月份的最大值和最小值
		for(String key:keys){
			
			if(minDate==null){
				minDate=key;
				maxDate=key;
			}
			if(DateUtil.compare(minDate, key, "yyyy-MM")){
				minDate=key;
				
			}
			if(DateUtil.compare(key, maxDate, "yyyy-MM")){
				maxDate=key;
				
			}
			
			
		}
		System.out.println("minDate:"+minDate);
		System.out.println("maxDate:"+maxDate);
		String[] month_arrays=DateUtil.getDatesBetweenTwoDate(minDate, maxDate, "yyyy-MM", "month");
		//忽略的月份 为了保证页面的连续性
		String lose_month="";
		if((maxDate.equals(dues_month_min)||maxDate.equals(dues_month_max))&&!"1".equals(param.getPage())){
			lose_month=maxDate;
		}
		if(month_arrays!=null){
			System.out.println("month_arrays'slength:"+month_arrays.length);
			for(int i=month_arrays.length-1;i>=0;i--){
				if(!lose_month.equals(month_arrays[i])){
					Dues_Month dues_Month=new Dues_Month();
					dues_Month.setFlag("1");
					dues_Month.setDues_month(month_arrays[i]);
					object_list.add(dues_Month);
				}
				
				if(month_map.containsKey(month_arrays[i])){
					for(Pb_Party_Dues dues:list){
						if(month_arrays[i].equals(dues.getDues_month())){
							App_Party_Dues app_Party_Dues=new App_Party_Dues();
							app_Party_Dues.setFlag("2");
							app_Party_Dues.setDues_id(dues.getDues_id());
							app_Party_Dues.setUser_name(dues.getUser_name());
							app_Party_Dues.setParty_dept_name(dues.getParty_dept_name());
							app_Party_Dues.setDues_val(dues.getDues_val().toString());
							object_list.add(app_Party_Dues);
						}
					}
				}
			}
		}
		session.setAttribute("dues_month_min", minDate);
		session.setAttribute("dues_month_max", maxDate);
		return new JsonObjectResult(result.getCount(),object_list);
	}

	public JsonObjectResult findAppDetailDuesByParam(BaseParam param, HttpServletRequest request)
			throws Pb_Party_DuesNotFoundException {
		JsonObjectResult result=findByParam(param, request);
		HttpSession session=request.getSession();
		String dues_month_min=(String)session.getAttribute("dues_month_min");
		String dues_month_max=(String)session.getAttribute("dues_month_max");
		List<Pb_Party_Dues> list=(List<Pb_Party_Dues>)result.getData();
		if(list.size()==0){
			return result;
		}
		
		Map<String, String> month_map=new HashMap<String, String>();
		
		List<Object> object_list=new ArrayList<Object>();
		//遍历list 获得缴纳党费信息的缴纳月份
		for(Pb_Party_Dues dues:list){
			if(!month_map.containsKey(dues.getDues_month())){
				month_map.put(dues.getDues_month(), dues.getDues_month());
			}
		}
		String minDate=null;
		String maxDate=null;
		Set<String> keys=month_map.keySet();
		//获得缴纳月份的最大值和最小值
		for(String key:keys){
			if(minDate==null){
				minDate=key;
				maxDate=key;
			}
			if(DateUtil.compare(minDate, key, "yyyy-MM")){
				minDate=key;
				
			}
			if(DateUtil.compare(key, maxDate, "yyyy-MM")){
				maxDate=key;
				
			}
			
		}
		String[] month_arrays=DateUtil.getDatesBetweenTwoDate(minDate, maxDate, "yyyy-MM", "month");
		//忽略的月份 为了保证页面的连续性
		String lose_month="";
		if((maxDate.equals(dues_month_min)||maxDate.equals(dues_month_max))&&!"1".equals(param.getPage())){
			lose_month=maxDate;
		}
		if(month_arrays!=null){
			System.out.println("month_arrays'slength:"+month_arrays.length);
			for(int i=month_arrays.length-1;i>=0;i--){
				if(!lose_month.equals(month_arrays[i])){
					Dues_Month dues_Month=new Dues_Month();
					dues_Month.setFlag("1");
					dues_Month.setDues_month(month_arrays[i]);
					object_list.add(dues_Month);
				}
				if(month_map.containsKey(month_arrays[i])){
					for(Pb_Party_Dues dues:list){
						if(month_arrays[i].equals(dues.getDues_month())){
							App_Party_DuesDetail entity=new App_Party_DuesDetail();
							entity.setFlag("2");
							entity.setDues_id(dues.getDues_id());
							entity.setUser_name(dues.getUser_name());
							entity.setParty_dept_name(dues.getParty_dept_name());
							entity.setDues_val(dues.getDues_val().toString());
							entity.setPay_flag(dues.getPay_flag_name());
							object_list.add(entity);
						}
					}
				}
			}
		}
		
		return new JsonObjectResult(result.getCount(),object_list);
	}

	public JsonObjectResult findUnifiedOrderDuesByParam(BaseParam param, HttpServletRequest request)
			throws Pb_Party_DuesNotFoundException {
		
		/*HttpSession session=request.getSession();
		String party_dept_id=(String)session.getAttribute("dept_id");
		if(party_dept_id==null||party_dept_id.trim().isEmpty()){
			throw new Pb_Party_DuesNotFoundException("操作员不是党员");
		}
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		checkParam(param0);
		if (param0.getDues_id() == null || param0.getDues_id().trim().isEmpty()) {
			throw new Pb_Party_DuesNotFoundException("请选择要缴费的党费");
		}
		Pb_Party_DuesParam param1=new Pb_Party_DuesParam();
		//查询要缴纳的党费是否存在
		param1.setDues_id(param0.getDues_id());
		String count=pb_Party_DuesDao.getCountByParam(param1);
		param1.setPay_flag("2");
		String count1=pb_Party_DuesDao.getCountByParam(param1);
		if("1".equals(count)||!"0".equals(count1)){
			throw new Pb_Party_DuesNotFoundException("所缴党费已缴费成功或不存在,缴费失败");
		}*/
		WXPay wxPay;
		WXPayConfigImpl wxPayConfigImpl=new WXPayConfigImpl();
		try {
			wxPay = new WXPay(wxPayConfigImpl,"https://202.100.219.99:8443/test.html",true,true);
			Map<String, String> reqData=new HashMap<String, String>();
			reqData.put("appid", "wxd2ffd5741d17c67d");
			reqData.put("mch_id", "10000100");
			reqData.put("nonce_str", WXPayUtil.generateNonceStr());
			reqData.put("sign_type", "MD5");
			
			reqData.put("body", "JSAPI支付测试");
			reqData.put("out_trade_no", "2018103100000018");
			reqData.put("total_fee", "1");
			reqData.put("spbill_create_ip", "192.168.43.159");
			reqData.put("notify_url", "https://202.100.219.99:8443/test.html");
			reqData.put("trade_type", "JSAPI");
			
			reqData.put("sign", WXPayUtil.generateSignature(reqData, "c15900faa2d1de5c6676d9add970a5e1"));
			//Map<String, String> result=wxPay.fillRequestData(reqData);
			Map<String, String> result=wxPay.unifiedOrder(reqData);
			return new JsonObjectResult(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public JsonObjectResult findAppMyDuesByParam(BaseParam param, HttpServletRequest request)
			throws Exception {
		
		Pb_Party_DuesParam param0=(Pb_Party_DuesParam)param;
		param0.setOrder("3");
		//获取当前最大的党费月份
		String max_dues_month=null;
		
		if(param0.getEnd_year()==null||param0.getEnd_year().trim().isEmpty()){
			max_dues_month=pb_Party_DuesDao.getMaxDuesMonth(param0.getUser_id());
			System.out.println(param0.getUser_id());
			System.out.println("打桩111111111111111");
		}else{
			max_dues_month=param0.getEnd_year();
			System.out.println("打桩2222222222222222");
		}
		System.out.println("max_dues_month:"+max_dues_month);
		if(max_dues_month==null){
			return new JsonObjectResult(new ArrayList());
		}
		int max_dues_year=Integer.valueOf(max_dues_month.substring(0,4));
		//获得党费年份的跨度
		int span=Integer.valueOf(PropertiesUtil.readProperties("db/config.properties", "dues_year_span"));
		String[]months={"01","02","03","04","05","06","07","08","09","10","11","12"};
		String[] year_months=StringUtil.getYearSpanMonth(max_dues_year-span+1, span, months);
		Map<String, App_MyParty_Dues> map=new HashMap<String, App_MyParty_Dues>();
		List<App_MyParty_DuesNew> result_list=new ArrayList<App_MyParty_DuesNew>();
		int temp=max_dues_year;
		//先为各个年份填空数据
		for(int i=0;i<span;i++){
			max_dues_year=temp-i;
			String dues_year=String.valueOf(max_dues_year);
			App_MyParty_DuesNew app_MyParty_DuesNew=new App_MyParty_DuesNew();
			app_MyParty_DuesNew.setYear(dues_year);
			List<App_MyParty_Dues> myParty_Dues=new ArrayList<App_MyParty_Dues>();
			
			for(String year_month:year_months){
				if(year_month.indexOf(dues_year)==0){
					App_MyParty_Dues app_MyParty_Dues=new App_MyParty_Dues();
					String map_month=year_month.substring(4);
					if(map_month.matches("0[1-9]")){
						map_month=map_month.substring(1);
					}
					app_MyParty_Dues.setMonth(map_month);
					myParty_Dues.add(app_MyParty_Dues);
				}
			}
			app_MyParty_DuesNew.setChildBeans(myParty_Dues);
			result_list.add(app_MyParty_DuesNew);
			
			
		}
		
		JsonObjectResult result=findByParam(param0, request);
		List<Pb_Party_Dues> list=(List<Pb_Party_Dues>)result.getData();
		if(list.size()==0){
			return new JsonObjectResult(result.getCount(),result_list);
		}
		Map<String, String> year_map=new HashMap<String, String>();
		List<Remind> reminds=new ArrayList<Remind>();
		//获取党费年份的提醒表信息
		for(int i=0;i<span;i++){
			max_dues_year=temp-i;
			RemindParam remindParam=new RemindParam();
			remindParam.setYear(String.valueOf(max_dues_year));
			reminds.addAll(remindDao.findRemindByParam(remindParam));
		}
		
		for(App_MyParty_DuesNew duesNew:result_list){
			for(Pb_Party_Dues entity:list){
				Pb_Party_Dues_Pay_Log log=entity.getDues_log();
				Timestamp pay_ok_date=null;
				if(log!=null){
					pay_ok_date=log.getPay_ok_date();
				}else{
					for(Remind remind:reminds){
						if(entity.getDues_month().equals(remind.getDues_month())){
							pay_ok_date=Timestamp.valueOf(remind.getDt_limit());
							break;
						}
					}
				}
				String year=entity.getDues_month().substring(0, 4);
				if(year.equals(duesNew.getYear())){
					String month=entity.getDues_month().substring(4);
					if(month.matches("0[1-9]")){
						month=month.substring(1);
					}
					List<App_MyParty_Dues> myParty_Dues=duesNew.getChildBeans();
					for(int i=0;i<myParty_Dues.size();i++){
						App_MyParty_Dues app_MyParty_Dues=myParty_Dues.get(i);
						if(month.equals(app_MyParty_Dues.getMonth())){
							app_MyParty_Dues.setDues_id(entity.getDues_id());
							app_MyParty_Dues.setState(entity.getState());
							app_MyParty_Dues.setUser_id(entity.getUser_id());
							app_MyParty_Dues.setUser_name(entity.getUser_name());
							app_MyParty_Dues.setPay_flag(entity.getPay_flag().toString());
							app_MyParty_Dues.setPay_flag_name(entity.getPay_flag_name());
							app_MyParty_Dues.setDues_val(entity.getDues_val().toString());
							app_MyParty_Dues.setDues_type(entity.getDues_type().toString());
							app_MyParty_Dues.setCreate_date(entity.getCreate_date());
							app_MyParty_Dues.setPay_ok_date(pay_ok_date);
							myParty_Dues.set(i, app_MyParty_Dues);
							break;
						}
						
					}
				}
				
			}
		}
		
		/*Set<String> keys=year_map.keySet();
		for(String key:keys){
			App_MyParty_DuesNew app_MyParty_DuesNew=new App_MyParty_DuesNew();
			List<App_MyParty_Dues> dues_list=new ArrayList<App_MyParty_Dues>();
			for(Pb_Party_Dues entity:list){
				Pb_Party_Dues_Pay_Log log=entity.getDues_log();
				Timestamp pay_ok_date=null;
				if(log!=null){
					pay_ok_date=log.getPay_ok_date();
				}else{
					for(Remind remind:reminds){
						if(entity.getDues_month().equals(remind.getDues_month())){
							pay_ok_date=Timestamp.valueOf(remind.getDt_limit());
							break;
						}
					}
				}
				String year=entity.getDues_month().substring(0, 4);
				String month=entity.getDues_month().substring(4);
				if(month.matches("0[1-9]")){
					month=month.substring(1);
				}
				if(key.equals(year)){
					App_MyParty_Dues dues=new App_MyParty_Dues();
					dues.setDues_id(entity.getDues_id());
					dues.setState(entity.getState());
					dues.setUser_id(entity.getUser_id());
					dues.setUser_name(entity.getUser_name());
					dues.setPay_flag(entity.getPay_flag().toString());
					dues.setPay_flag_name(entity.getPay_flag_name());
					dues.setDues_val(entity.getDues_val().toString());
					dues.setDues_type(entity.getDues_type().toString());
					dues.setMonth(month);
					dues.setCreate_date(entity.getCreate_date());
					dues.setPay_ok_date(pay_ok_date);
					dues_list.add(dues);
					
				}
				
			}
			app_MyParty_DuesNew.setYear(key);
			app_MyParty_DuesNew.setChildBeans(dues_list);
			result_list.add(app_MyParty_DuesNew);
		}*/
		return new JsonObjectResult(result.getCount(),result_list);
	}
	
	
	

	

}
